package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.com.kunpeng.entity.BrandCategory;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.BrandCategoryQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.BrandCategoryMapper;
import org.jeecg.modules.demo.com.kunpeng.service.IBrandCategoryService;
import org.jeecg.modules.demo.com.kunpeng.vo.BrandCategoryQueryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 商品类别
 * @Author: jeecg-boot
 * @Date:   2023-01-12
 * @Version: V1.0
 */
@Service
public class BrandCategoryServiceImpl extends ServiceImpl<BrandCategoryMapper, BrandCategory> implements IBrandCategoryService {

    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    /**
     * 新增
     * @param brandCategory
     */
    @Override
    public void insert(BrandCategory brandCategory) {
        List<BrandCategory> brandCategories = brandCategoryMapper.queryListByClassificationName(brandCategory.getClassificationName());
        if(brandCategories.size()>0){
            String message="该商品类别已存在，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        brandCategory.setState("0");
        brandCategoryMapper.insert(brandCategory);
    }

    /**
     * 根据类别名称模糊查询
     * @param page
     * @param brandCategoryQueryListDTO
     * @return
     */
    @Override
    public Page<BrandCategoryQueryListVO> queryListByCategoryName(Page<BrandCategoryQueryListVO> page, BrandCategoryQueryListDTO brandCategoryQueryListDTO) {
        Page<BrandCategoryQueryListVO> brandCategoryQueryListVOPage = page.setRecords(brandCategoryMapper.queryListByCategoryName(page, brandCategoryQueryListDTO.getCategoryKeyWords()));
        return brandCategoryQueryListVOPage;
    }

    /**
     * 根据类别名称模糊查询全部
     * @param page
     * @param brandCategoryQueryListDTO
     * @return
     */
    @Override
    public Page<BrandCategoryQueryListVO> queryListByCategoryNameAll(Page<BrandCategoryQueryListVO> page, BrandCategoryQueryListDTO brandCategoryQueryListDTO) {
        Page<BrandCategoryQueryListVO> brandCategoryQueryListVOPage = page.setRecords(brandCategoryMapper.queryListByCategoryNameAll(page, brandCategoryQueryListDTO.getCategoryKeyWords()));
        return brandCategoryQueryListVOPage;
    }

    /**
     * 启用商品类别
     */
    @Override
    public void enableCategory(String id) {
        int num = brandCategoryMapper.enableCategory(id);
        if(num!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"启用失败，请联系管理员！");
        }
    }

    /**
     * 禁用商品类别
     */
    @Override
    public void unEnableCategory(String id) {
        int num = brandCategoryMapper.unEnableCategory(id);
        if(num!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"禁用失败，请联系管理员！");
        }
    }
}
